package com.linciping.treeview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class AreaAdapter extends TreeAdapter<AreaAdapter.AreaViewHolder, AreaNode> {

    private final Context context;

    public AreaAdapter(Context context, List<AreaNode> nodeList) {
        super(nodeList);
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull AreaViewHolder holder, int position, AreaNode node) {
        holder.tvAreaName.setText(node.getName());
        if (isShowExpand(node)) {
            holder.ivExpand.setVisibility(View.VISIBLE);
        } else {
            holder.ivExpand.setVisibility(View.INVISIBLE);
        }
        holder.ivExpand.setOnClickListener(v -> {
            setExpand(node, position);
        });
    }

    @NonNull
    @Override
    public AreaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AreaViewHolder(LayoutInflater.from(context).inflate(R.layout.item_area, parent, false));
    }

    static class AreaViewHolder extends RecyclerView.ViewHolder {

        TextView tvAreaName;
        ImageView ivExpand;

        AreaViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAreaName = itemView.findViewById(R.id.tv_area_name);
            ivExpand = itemView.findViewById(R.id.iv_expand);
        }
    }
}
